package leetcode.code2166;

class Bitset {

	int[] set;
	int cnt1;
	int size;
	boolean flip;

	public Bitset(int size) {
		set = new int[size];
		cnt1 = 0;
		this.size = size;
		flip = false;
	}

	public int numOne() {
		return flip ? 0 : 1;
	}

	public int numZero() {
		return flip ? 1 : 0;
	}

	public void fix(int idx) {
		int one = this.numOne();
		if (set[idx] != one) {
			set[idx] = one;
			cnt1++;
		}

	}

	public void unfix(int idx) {
		int zero = this.numZero();
		if (set[idx] != zero) {
			set[idx] = zero;
			cnt1--;
		}
	}

	public void flip() {
		flip = !flip;
		cnt1 = size - cnt1;
	}

	public boolean all() {
		return cnt1 == size;
	}

	public boolean one() {
		return cnt1 > 0;
	}

	public int count() {
		return cnt1;
	}

	public String toString() {
		StringBuffer ans = new StringBuffer();
		if (flip) {
			for (int i = 0; i < set.length; i++) {
				ans.append(1 - set[i]);
			}
		} else {
			for (int i = 0; i < set.length; i++) {
				ans.append(set[i]);
			}
		}
		return ans.toString();
	}
}
